<template>
  <div>
    <list ref="list"
          :showRefresh="true"
          :showLoadMore="showLoadMore"
          @refresh="refresh"
          @loadMore="loadmore"
          loadmoreoffset="100">
      <cell v-for="(item, index) in coops"
            :key="index">
        <div class="bg-white border-bottom p-3">
          <text class="title">{{item.service_name}}</text>
          <text class="remark">地址：{{item.address}}</text>
          <text class="remark">联系人：{{item.res_person_name}}</text>
          <text class="remark"
                @click="call(item.res_phone_number)">联系电话：{{item.res_phone_number}}</text>
        </div>
      </cell>
    </list>
  </div>
</template>
<script>
import { STORAGE_FARM } from '../../plugins/constant'

export default {
  components: {},
  props: {},
  data: () => ({
    farm: {},
    coops: [],
    totalPages: 2,
    params: {
      index: 1,
      size: 10,
      'supplier_id.eq': ''
    }
  }),
  computed: {
    showLoadMore() {
      return this.totalPages > this.params.index
    }
  },
  watch: {},
  created() {
    this.getFarm()

  },
  methods: {
    getFarm() {
      this.farm = this.$storage.getSync(STORAGE_FARM) || {}
      if (this.farm.id) {
        this.params['supplier_id.eq'] = this.farm.id
        this.refresh()
      } else {
        this.$notice.toast({ message: '没有鸡场信息' })
        this.$router.setBackParams({ type: 1 })
      }
    },
    refresh() {
      this.params.index = 1
      this.getData().then(res => {
        this.coops = res
      }).finally(() => this.$refs["list"].refreshEnd())
    },
    loadmore() {
      this.getData().then(res => {
        this.coops.push(...res)
      }).finally(() => this.$refs["list"].loadMoreEnd())
    },
    getData() {
      return new Promise((resolve, reject) => {
        this.$api.getData({
          server: 'agr_chain',
          table: 'supplier_related_view_for_app',
          params: this.params,
          errorMsg: '获取标准数据失败'
        }).then(res => {
          this.params.index++
          this.totalPages = res && res.totalPages || 0
          resolve(res && res.data || [])
        }).catch(err => {
          this.totalPages = 0
        })
      })
    },
    call(phone) {
      this.$coms.call(phone, false)
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../css/variables.scss";

.bg-white {
  background-color: #fff;
}
.p-3 {
  padding: 30px;
}
.border-bottom {
  border-bottom-width: 1px;
  border-bottom-color: $border-color;
}
.desc {
  font-size: 28px;
  color: #666;
}
.remark {
  font-size: 26px;
  color: #999;
}
</style>
